package com.zyy.smartcity1129.movie;

import android.net.Uri;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieNextFilm;
import com.zyy.smartcity1129.util.Http;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: MovieNextFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 21/11/2021$
 * @Version: 1.0
 */
public class MovieNextFragment extends BaseFragment {
    private String movieNextName = MovieMovieFragment.movieNextName;
    private int movieNextId = MovieMovieFragment.movieNextId;
    private ImageView imgMovieContext1;
    private TextView tvMovieContext1;
    private TextView tvMovieContext2;
    private TextView tvMovieContext3;
    private LinearLayout line1, lineMoveContext1, lineMoveContext2, lineMoveContext3;
    private LinearLayout line2;
    private TextView tvMovieContext4;
    private TextView tvMovieContext5;
    private WebView webViewMovieContext;
    private LinearLayout lineMovieContext1;
    private TextView textView19;
    private TextView textView20;
    private LinearLayout lineMovieContext2;
    private VideoView videoViewMovieContext;
    private ImageView imgMovieContext2;
    private TextView tvMovieContext21;
    private RecyclerView rvMovieContext;
    ExecutorService executor = Executors.newFixedThreadPool(1);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    MovieNextFilm.DataBean dataBean;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_context;
    }

    @Override
    protected String title() {
        return movieNextName;
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                dataBean = Http.getInstance().get("/prod-api/api/movie/film/preview/" + movieNextId, null, MovieNextFilm.class, 1).getData();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 1) {
                Glide.with(imgMovieContext1.getContext()).load(Http.base + dataBean.getCover()).into(imgMovieContext1);
                Glide.with(imgMovieContext2.getContext()).load(Http.base + dataBean.getCover()).into(imgMovieContext2);
                tvMovieContext1.setText(dataBean.getName());
                tvMovieContext2.setVisibility(View.GONE);
                tvMovieContext3.setText(dataBean.getPlayDate() + "中国大陆上映");
                lineMoveContext1.setVisibility(View.GONE);
                lineMoveContext2.setVisibility(View.GONE);
                lineMoveContext3.setVisibility(View.GONE);
                rvMovieContext.setVisibility(View.GONE);
                lineMovieContext1.setVisibility(View.VISIBLE);
                lineMovieContext2.setVisibility(View.VISIBLE);
                imgMovieContext2.setVisibility(View.GONE);
                videoViewMovieContext.setVisibility(View.VISIBLE);
                webViewMovieContext.loadDataWithBaseURL(Http.base, dataBean.getIntroduction(), "text/html", "utf-8", null);
                textView19.setTextColor(getResources().getColor(R.color.black));
                textView20.setTextColor(getResources().getColor(R.color.gray));
//                网络视频源报错
//                String url = Http.base + dataBean.getVideo();
//                videoViewMovieContext.setVideoURI(Uri.parse(url));
                videoViewMovieContext.setVideoURI(Uri.parse("android.resource://"+ getContext().getPackageName()+"/" + R.raw.mp41));
                MediaController mediaController = new MediaController(getContext());
                mediaController.setMediaPlayer(videoViewMovieContext);
                videoViewMovieContext.setMediaController(mediaController);
                videoViewMovieContext.requestFocus();
                videoViewMovieContext.start();
                textView19.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        imgMovieContext2.setVisibility(View.GONE);
                        videoViewMovieContext.setVisibility(View.VISIBLE);
                        videoViewMovieContext.start();
                        textView19.setTextColor(getResources().getColor(R.color.black));
                        textView20.setTextColor(getResources().getColor(R.color.gray));
                    }
                });
                textView20.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        videoViewMovieContext.pause();
                        imgMovieContext2.setVisibility(View.VISIBLE);
                        videoViewMovieContext.setVisibility(View.GONE);
                        textView20.setTextColor(getResources().getColor(R.color.black));
                        textView19.setTextColor(getResources().getColor(R.color.gray));
                    }
                });
            }
        });
    }

    private void initView(View v) {
        imgMovieContext1 = (ImageView) v.findViewById(R.id.imgMovieContext1);
        tvMovieContext1 = (TextView) v.findViewById(R.id.tvMovieContext1);
        tvMovieContext2 = (TextView) v.findViewById(R.id.tvMovieContext2);
        tvMovieContext3 = (TextView) v.findViewById(R.id.tvMovieContext3);
        line1 = (LinearLayout) v.findViewById(R.id.line1);
        line2 = (LinearLayout) v.findViewById(R.id.line2);
        tvMovieContext4 = (TextView) v.findViewById(R.id.tvMovieContext4);
        tvMovieContext5 = (TextView) v.findViewById(R.id.tvMovieContext5);
        webViewMovieContext = (WebView) v.findViewById(R.id.webViewMovieContext);
        textView19 = (TextView) v.findViewById(R.id.textView19);
        textView20 = (TextView) v.findViewById(R.id.textView20);
        videoViewMovieContext = (VideoView) v.findViewById(R.id.videoViewMovieContext);
        imgMovieContext2 = (ImageView) v.findViewById(R.id.imgMovieContext2);
        tvMovieContext21 = (TextView) v.findViewById(R.id.tvMovieContext21);
        rvMovieContext = (RecyclerView) v.findViewById(R.id.rvMovieContext);
        lineMovieContext1 = (LinearLayout) v.findViewById(R.id.lineMovieContext1);
        lineMovieContext2 = (LinearLayout) v.findViewById(R.id.lineMovieContext2);
        lineMoveContext1 = (LinearLayout) v.findViewById(R.id.lineMoveContext1);
        lineMoveContext2 = (LinearLayout) v.findViewById(R.id.lineMoveContext2);
        lineMoveContext3 = (LinearLayout) v.findViewById(R.id.lineMoveContext3);
    }
}
